package com.fbote.base;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDAO extends HibernateDaoSupport implements IBaseDAO {
	
	public Session getsession(){
		return this.getSession();
	}
	
	public boolean delete(Object obj) {
		Session session = getsession();
		Transaction tran = session.beginTransaction();
		try{
			session.delete(obj);
			tran.commit();
			return true ;
		}catch (DataAccessException e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			session.close();
		}
		return false ;
	}

	public Object findById(Class clazz, Integer id) {
		Session session = getSession() ;
		Object obj = session.get(clazz, id);
		session.close();
		
		return obj ;
	}

	public Object HQLByObject(String SQL) {
		Session session = getSession();
		Object o = session.createQuery(SQL).list();
		session.close();
		return o ;
	}

	public boolean save(Object obj) {
		Session session = getsession();
		Transaction tran = session.beginTransaction();
		try{
			session.save(obj);
			tran.commit();
			return true;
		}catch (DataAccessException e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			session.close();
		}
		return false ;
	}

	public boolean update(Object obj) {
		Session session = getsession();
		Transaction tran = session.beginTransaction();
		try{
			session.update(obj);
			tran.commit();
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			session.close();
		}
		return false;
	}

}
